<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>vue</title>
</head>

<body>
<div id="name"></div>
<div id="age"></div>
<script>
  let name = document.getElementById('name');
  let age = document.getElementById('age');
  class Dep {
    subs = []
    addSub(sub) {
      this.subs.push(sub);
    }
    notify() {
      this.subs.forEach((sub) => sub())
    }
  }
  function observe(target) {
    Object.keys(target).forEach((key) => {
      let val = target[key];
      const dep = new Dep();
      if (key == 'name') {
        name.innerHTML = val;
        dep.addSub(() => { name.innerHTML = val });
      } else if (key == 'age') {
        age.innerHTML = val;
        dep.addSub(() => { age.innerHTML = val });
      }
      Object.defineProperty(target, key, {
        get: function () {
          return val;
        },
        set: function (value) {
          val = value;
          dep.notify();
        }
      });
    })
  }

  let obj = { name: '名称', age: '年龄' };
  observe(obj);
  setTimeout(() => {
    obj.name = '新名称';
  }, 3000);
  setTimeout(() => {
    obj.age = '新年龄';
  }, 6000);
</script>
</body>
</html>
